
Game Engine Commands
--------------------


Overview
--------
The game engine processes message buffers with the following format:

[CommandPacket]

Offset 	Size 	Description
------ 	---- 	-----------
0x0	4	messageType
0x4	2	dataLength [length of type specific message data]
0x6	4	timeStamp  [tick]
0xA	4	**Unknown**
------
0xE	X	Type specific message data [of length: 'dataLength']


Note: When being sent over the network, the header data is stored slightly differntly. It uses the following format.

[NetworkCommandPacket]

Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	playerNum
0x1 	4 	timeStamp
---------------------------
0x5	Y	CommandPacketInfo[]
 ----------------
 0x5+Y 	 1	 messageType
 0x6+Y 	 1 	 dataLength
 0x7+Y 	 4 	 **Unknown** (same **Unknown** as above format)
 ----------------
 0xB+Y	 X	 Type specific message data (of lengh: 'dataLengh')



The following table lists the meaning of various game engine message types. A table containing the coresponding text string (addresses) can be found at address 4E1208. Many of these messages are processed by a routine at address 0040E300. Note that this function appears to be part of the internal player class. Any out of range values cause this function to return 0. A number of cases do nothing but return 1 (these cases are 0, 29, 2C, 2D, 2E).

Case 	Game Text String 	Description
---- 	---------------- 	-----------
0x00	ctNop	 		NOP - No Operation
0x01	ctMoDoze 		Bulldoze region
0x02	ctMoMove 		Move
0x03	ctMoDock		Move Unit to Dock
0x04	ctMoDockEG		Dock at Garage
0x05	ctMoStop		Stop (unit, lab research, university train, Structure Factory build)
0x06	ctMoBuild		Build (ConVec, RoboMiner, GeoCon)
0x07	ctMoBuildWall		Build tube/wall (normal/microbe/lava)
0x08	ctMoRemoveWall		Destroy tube/wall (normal/microbe/lava)
0x09	ctMoProduce		Build (StructureKit/Consumer Items/Vehicle/SpaceportKit)
0x0A	ctMoTransferCargo	Load/Unload at Structure Factory, Spaceport, Garage, Rocket/RLV
0x0B	ctMoLoadCargo		Load Cargo (into Cargo Truck)
0x0C	ctMoUnloadCargo		Unload Cargo (from Cargo Truck)
0x0D	ctMoRecycle		Recycle
0x0E	ctMoDumpCargo		Dump cargo
0x0F	ctMoScavenge		(return 0)
0x10	ctMoSpecialWait		(return 0)
0x11	ctMoSurvey		(return 0)
0x12	ctMoIdle		Idle Building
0x13	ctMoUnidle		Activate Building
0x14	ctMoSelfDestruct	Self Destruct
0x15	ctMoScatter		Scatter group
0x16	ctMoResearch		Research/Assign scientists
0x17	ctMoTrainScientists	Train scientists
0x18	ctMoTransfer		Transfer
0x19	ctMoLaunch		Launch (SULV/RLV/Rocket)
0x1A	ctMoFlyInSpace		(return 0)
0x1B	ctMoRepair		(return 0)
0x1C	ctMoRepairObj		Repair building/unit
0x1D	ctMoReprogram		Reprogram unit
0x1E	ctMoDismantle		Dismantle building
0x1F	ctMoSalvage		Salvage rubble (set region and route)
0x20	ctMoCreate		
0x21	ctMoDevelop		(return 0)
0x22	ctMoUnDevelop		(return 0)
0x23	ctMoLightToggle		Set lights (on/off)
0x24	ctMoAttackObj		Attack/Detonate (not self destruct)
0x25	ctMoGuard		Guard unit
0x26	ctMoStandGround		Stand ground
0x27	ctMoCargoRoute		Set ore route
0x28	ctMoPatrol		Set patrol route
0x29	ctMapChange		
0x2A	ctMoPoof		Exit map
0x2B	ctGameOpt		Set game engine variable (return 0)
0x2C	ctGodWeapon		
0x2D	ctChatText		
0x2E	ctChatSFX		
0x2F	ctMoDeath		(return 0)
0x30	ctChat			Text message
0x31	ctQuit			Player quitting
0x32	ctAlly			
0x33	ctGoAI			
0x34	ctMachineSettings	
0x35	ctInvalidCommand	
0x36	WeaponFiring		





Specific Message Data Formats
-----------------------------
Following is a description of the data portion of the game engine messages according to the message type.


Type: (0x01) "ctMoDoze" Bulldoze region
------------
Size in bytes: 11 minimum (9 + numUnits*2)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits - Number of unitIDs in the packet
0x1 	2*X 	unitID - array of size numUnits of units being moved
0x3+2X 	2 	bulldozeRegion.TopLeft.tileX
0x5+2X 	2 	bulldozeRegion.TopLeft.tileY
0x7+2X 	2 	bulldozeRegion.BottomRight.tileX
0x9+2X 	2 	bulldozeRegion.BottomRight.tileY


Type: (0x02) "ctMoMove" Move a unit or group of units
Type: (0x03) "ctMoDock" Dock (move unit to dock)
Type: (0x04) "ctMoDockEG" Dock at Garage
Type: (0x26) "ctMoStandGround" Stand ground
Type: (0x27) "ctMoCargoRoute" Set ore route
Type: (0x28) "ctMoPatrol" Set patrol route
------------
Some types have more bytes at the end.
Size in bytes: 9 (minimum), 7+2*numUnits
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits - Number of unitIDs in the packet
0x1 	2*X 	unitID - array of size numUnits of units being moved
0x3+2X 	2 	numWayPoints (BYTE)
0x5+2X 	4*Y 	destination (pixelX:15 [low], pixelY:14 [high])

CargoRoute (additional fields)
----------
 	2 	smelterWaypointIndex
 	2 	mineWaypointIndex
 	2 	mineUnitID
 	2 	smelterUnitID

Patrol (additional fields)
------
	2	???
	2	???


Type: (0x05) "ctMoStop" Stop (unit, lab research, university train, Structure Factory build)
------------
Size in bytes: 3 (minimum) 1 + numUnits*2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits


Type: (0x06) "ctMoBuild" Build a building (ConVec, RoboMiner, GeoCon)
------------
Size in bytes: 	0x13 (19 bytes) minimum to deploy RoboMiner/GeoCon/ConVec building
Size in bytes: 	0xD + numUnits*2 + numWayPoints*4 
		(13 + numUnits*2 + numWayPoints*4)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits
0x3 	2 	numWayPoints (BYTE)
0x5 	4*Y 	wayPointLocation - (pixelX:15, pixelY:14)
0x9 	2 	buildArea.TopLeft.x (bulldozed tile)
0xB 	2 	buildArea.TopLeft.y (bulldozed tile)
0xD 	2 	buildArea.BottomRight.x (bulldozed tile)
0xF 	2 	buildArea.BottomRight.y (bulldozed tile)
0x11 	2 	(Stored to Unit.+0x6A - value 0xFFFF)


Type: (0x07) "ctMoBuildWall" Build tube/wall/microbe wall/lava wall
------------
Size in bytes: 0x0F (15bytes) (minimum)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits
0x3+2X 	2 	tubeRegion.TopLeft.tileX (including tube)
0x5+2X 	2 	tubeRegion.TopLeft.tileY (including tube)
0x7+2X 	2 	tubeRegion.BottomRight.tileX (excluding tube)
0x9+2X 	2 	tubeRegion.BottomRight.tileY (excluding tube)
0xB+2X 	2 	map_id enum Tube/Wall type
0xD+2X 	2 	"0000"


Type: (0x08) "ctMoRemoveWall" Destroy tube/wall/microbe wall/lava wall
------------
Size in bytes: 0x11 (17 bytes) (minimum)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits
0x3+2X 	2 	numWayPoints
0x5+2X 	4*Y 	wayPointLocation - (pixelX:15, pixelY:14)
0x7+2X 	2 	tubeRegion.TopLeft.tileX (inclusive)
0x9+2X 	2 	tubeRegion.TopLeft.tileY (inclusive)
0xB+2X 	2 	tubeRegion.BottomRight.tileX (inclusive)
0xD+2X 	2 	tubeRegion.BottomRight.tileY (inclusive)


Type: (0x09) "ctMoProduce" Build (StructureKit/Consumer Items/Vehicle/SpaceportKit)
------------
Size in bytes: 8 (for Structure Factory)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	short factoryUnitID
0x2 	2 	enum map_id itemToProduce
0x4 	2 	enum map_id weaponType
0x6 	2 	short scStubIndex  [-1 if not used]


Type: (0x0A) "ctMoTransferCargo" Exchanges cargo at Structure Factory, Spaceport, Garage, Rocket/RLV with that of a CargoTruck
------------
Size in bytes: 6
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	short buildingUnitIndex
0x2 	2 	short bay
0x4 	2 	"0000"


Type: (0x0B) "ctMoLoadCargo" Load Cargo (into Cargo Truck)
------------
Size in bytes: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	unitID - of the building where loading takes place


Type: (0x0C) "ctMoUnloadCargo" Unload Cargo (from Cargo Truck)
------------
Size in bytes: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	unitID - of the building where unloading takes place


Type: 0x0D	ctMoRecycle
----------
Size: 4
Offset	Size	Description
------	----	-----------
0x0	2	short unitIndex
0x2	2	short ???


Type: 0x0E	ctMoDumpCargo
----------
Size: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	short unitIndex


Type: 0x12	ctMoIdle
----------
Size: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	short buildingUnitIndex


Type: 0x13	ctMoUnidle
----------
Size: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	short buildingUnitIndex


Type: (0x14) "ctMoSelfDestruct" Self Destruct (unit)
------------
Size: 3 (minimum) (1 + numUnits*2)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2 	unitID - array of units that are self destructing


Type: (0x15) "ctMoScatter" Scatter group
------------
Size in bytes: 3 (minimum) (1 + numUnits*2)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2 	unitID - array of units that are scattering


Type: (0x16) "ctMoResearch" Assign scientists to research
------------
Size in bytes: 6
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	unitID - of the lab where research is to be done
0x2 	2 	techNum - of the tech to be researched
0x4 	2 	numScientists - number of scientists assigned to research


Type: (0x17) "ctMoTrainScientists" Train scientists
------------
Size in bytes: 4
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	unitID - of the university where workers are trained into scientists
0x2 	2 	numTrained - number of people being trained


Type: (0x19) "ctMoLaunch"
------------
Size in bytes: 6
Offset	Size	Description
------	----	-----------
0x0	2	unitIndex
0x2	2	destPixelX
0x4	2	destPixelY


Type: (0x1C) "ctMoRepairObj" Repair building/unit
Type: (0x1D) "ctMoReprogram" Reprogram unit
Type: (0x1E) "ctMoDismantle" Dismantle building
------------
Size in bytes: 9
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits - units that do the repairs, reprograms, or dismantling
0x3+2X 	2 	"0000"
0x5+2X 	2 	unitID - target of action
0x7+2X 	2 	"FFFF"


Type: (0x20) "ctMoCreate"
------------
Size in bytes: 
Note: Used to create units
Offset	Size	Description
------	----	-----------
0x0	2	short numUnitsToCreate
0x2	12*X	CreateUnitInfo[]
 0x2	 4	 map_id unitType
 0x6	 2	 short tileX
 0x8	 2	 short tileY
 0xA	 4	 map_id weaponOrCargo


Type: (0x23) "ctMoLightToggle" Set lights (on/off)
------------
Size in bytes: 5
Note: If a group has both units with headlights on and units with headlights off, the group is considered to have units with headlights off (and are thus toggled to on)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits - units whose lights are set/toggled
0x3+2X 	2 	headLightState - set headlights to this state (0 - Off, 1 - On)


Type: (0x24) "ctMoAttackObj" Attack/Detonate (not self destruct)
Type: (0x25) "ctMoGuard" Guard unit
------------
Size in bytes: 9 (minimum) (7 + numUnits*2)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	numUnits
0x1 	2*X 	unitID - array of size numUnits - units doing the attacking
0x3+2X 	2 	"0000"
0x5+2X 	2 	unitID/tileX - target unitID or location tileX (unitID if attacking or guarding a unit, location if attacking ground)
0x7+2X 	2 	"FFFF"/tileY - (-1) or location tileY (-1 if attacking or guarding a unit, location if attacking ground)


Type: (0x2A) "ctMoPoof" Exit map
------------
Size in bytes: 2 (not tested, but only 2 bytes accessed by code)
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	2 	unitID - of unit exiting the map


Type: (0x2B) "ctGameOpt" Set game engine variable
------------
Size in bytes: 6+ (8 for Game speed)
Offset 	Size 	Description
------ 	---- 	-----------
0x0	2	unknown
0x2	2	variableIdentifier - determines which variable is set (8-32)
0x4	2	newValue - new value for variable selected (TethysGame.sIssueOptPacket)
--------------------------- (optional data used by some packets)
0x6	2	playerNum

The variable identifier takes the following values:
Value 	Description
----- 	-----------
0x8 	SetPlayerCommonOre
0x9 	SetPlayerRareOre
0xA 	SetPlayerScientists
0xB 	SetPlayerWorkers
0xC 	SetPlayerFoodStored
0xD 	SetPlayerKids
-------------------
0xE 	ForceDisableRobotCommandCenterEffect (for all human players)
0xF 	ForceEnableRobotcommandCenterEffect (for all human players)
-------------------
0x13	DaylightEverywhere (SetDaylightEverywhere)
0x14 	GameSpeed
-------------------
0x15	Force morale great
0x16	Force morale good
0x17	Force morale OK
0x18	Force morale poor
0x19	Force morale rotten (ForceMoraleRotten)
-------------------
0x1A 	IncreasePlayerResources (workers, scientists, kids, common ore, rare ore, food stored)
0x1B 	IncreaseAllPlayersResources (workers, scientists, kids, common ore, rare ore, food stored)
0x1C 	CreateVortex
0x1D 	CreateLightning
0x1E 	CreateEarthquake
0x1F 	CreateMeteor
0x20	Free morale level (FreeMoraleLevel)
-------------------
X	If not one of the above values, then set the DWORD at index (variableIdentifier) within the TethysGame global object to (**Unknown**) field in the command packet (sign extended to a DWORD)


Here are some of those "X" values from above.
-------------------
0x3	DoDamage4X
0x12	DaylightMoves (SetDaylightMoves)
0x21	GameTick
0x22	TickOfLastSetGameOpt (!=0 means "CHEATED GAME!") Note: This value is set to TethysGame::Tick immediately after setting it's new value, so in effect, it can't be modified here
0x24	LocalPlayerNum




Type: (0x30) "ctChat" Text message
------------
Size in bytes: 2 + messageLength
Offset 	Size 	Description
------ 	---- 	-----------
0x0	1	Source player number (who said it)
0x1	1	Destination players (bit flags, a 1 means that player gets the message)
0x2	X	Null terminated text string


Type: (0x31) "ctQuit" Player quitting
------------
Size in bytes: 2
Offset 	Size 	Description
------ 	---- 	-----------
0x0 	1 	quitMethod  [0 = SelfDestruct, 1 = TransferToAllies, other = GoAI]
0x1 	1 	lParam  ["02"]


Type: (0x32) "ctAlly"
------------
Size in bytes: 4
Offset	Size	Description
------	----	-----------
0x0	2 (1)	allyFromPlayerNum	[Write 2 bytes, never read]
0x2	2 (1)	allyToPlayerNum		[Write 2 bytes, read 1 byte]


Type: (0x33) "ctGoAI"
------------
Size: 0


Type: (0x34) "ctMachineSettings"
------------
Size: 8
Offset	Size	Description
------	----	-----------
0x0	2	cpuSpeed
0x2	2	totalPhysMem	[MB rounded up to multiple of 4]
0x4	2	windowWidth	[of Dans_RULE_UIFrame]
0x6	2	windowHeight	[of Dans_RULE_UIFrame]


Type: (0x35) "ctInvalidCommand"
------------
Type: (0x36) "WeaponFiring"
------------
